home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_200
/
299_01
/
conver.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-12-28
|
13KB
|
272 lines
/*
---------------------------------------------------------------------------
filename: conver.c
author: g. m. crews
creation date: 28-Jul-1988
date of last revision: 28-Jul-1989
this function calculates and returns the conversion factor required for
going from units represented by an input string of characters, to units
represented by an output string of characters.
to choose the proper strings for various units, see the conversion table
array of structures defined below. this table gives each unit's key,
conversion factor, string, and base unit's key. the conversion factor is
the number by which to multiply in order to convert to the given base unit.
notice that all compatible units have the same base key. thus, to
convert from units given by an input_key to units given by an output
key, the function simply returns the input conversion factor divided by
the output conversion factor.
if the input_key or output_key are not found in the conversion_table,
or the keys do not have the same base, 0. is returned.
conversion factors were obtained from ASTM E380-82 Standard for Metric
Practice.
example, to go from feet to inches:
conversion_factor = calc_conversion_factor("ft", "in");
---------------------------------------------------------------------------
*/
#include <string.h>
double calc_conversion_factor(char input_str[],char output_str[]);
#define NUM_CFACTORS_IN_TBL 160
#define MAX_LEN_UNITS 14
double calc_conversion_factor(
char input_str[], /* string describing input units (see table below) */
char output_str[]) /* string describing output units */
{
int i,j;
double factor1, factor2;
int base1, base2;
static struct conversion_factors {
int key; /* code number for this units */
double factor; /* multiplier to obtain base (reference) units */
int base; /* code number for base units */
char units[MAX_LEN_UNITS+1];
/* string describing units */
} conversion_table[NUM_CFACTORS_IN_TBL] = {
/* This is the array containing conversion factors. Note that an
askerisk (*) in the comment describing a unit means the conversion
factor is exact. */
/* LENGTH: */
{ 100, 1.000000E+00, 100, "m" }, /* *(meter) */
{ 101, 1.000000E-10, 100, "angstrom" },
{ 102, 1.495979E+11, 100, "ast-unit" }, /* astronomical unit */
{ 103, 2.011684E+01, 100, "chain" }, /* US survey ft */
{ 104, 1.828804E+00, 100, "fathom" }, /* US survey ft */
{ 105, 1.000000E-15, 100, "fermi" },
{ 106, 3.048000E-01, 100, "ft" }, /* *(foot) */
{ 107, 3.048006E-01, 100, "survey-ft" }, /* US survey ft */
{ 108, 2.540000E-02, 100, "in" }, /* *(inch) */
{ 109, 9.460550E+15, 100, "light-year" },
{ 110, 2.540000E-08, 100, "micro-in" }, /* *microinch */
{ 111, 1.000000E-06, 100, "micron" }, /* *micron */
{ 112, 2.540000E-05, 100, "mil" }, /* *mil */
{ 113, 1.852000E+03, 100, "naut-mi" }, /* nautical mile */
{ 114, 1.609347E+03, 100, "mi" }, /* statute mile
(US survey foot) */
{ 115, 3.085678E+16, 100, "parsec" },
{ 116, 4.217518E-03, 100, "pica" }, /* printer's */
{ 117, 3.514598E-04, 100, "point" }, /* *(printer's) */
{ 118, 5.029210E+00, 100, "rod" }, /* rod (US survey foot) */
{ 119, 9.144000E-01, 100, "yd" }, /* *yard */
/* ACCELERATION: */
{ 200, 1.000000E+00, 200, "m/s2" }, /* meter/sec2 */
{ 201, 3.048000E-01, 200, "ft/s2" },
{ 202, 9.806650E+00, 200, "g" }, /* *standard free-fall */
{ 203, 2.540000E-02, 200, "in/s2" }, /* *in/s2 */
/* ANGLE: */
{ 300, 1.000000E+00, 300, "rad" }, /* *rad */
{ 301, 1.745329E-02, 300, "deg" }, /* deg */
{ 302, 2.908882E-04, 300, "min" }, /* minute */
{ 303, 4.848137E-06, 300, "sec" }, /* second */
{ 304, 1.570796E-02, 300, "grad" }, /* grad */
/* AREA: */
{ 400, 1.000000E+00, 400, "m2" }, /* m2 */
{ 401, 4.046873E+03, 400, "acre" }, /* acre (US survey foot) */
{ 402, 9.290304E-02, 400, "ft2" }, /* *ft2 */
{ 403, 1.000000E+04, 400, "ht" }, /* hectare */
{ 404, 6.451600E-04, 400, "in2" }, /* *in2 */
{ 405, 2.589998E+06, 400, "mi2" }, /* US statute */
{ 406, 8.361274E-01, 400, "yd2" },
/* TORQUE: */
{ 500, 1.000000E+00, 500, "N*m" }, /* *(newton meter) */
{ 501, 1.000000E-07, 500, "dyne*cm" }, /* *dyne*cm */
{ 502, 9.806650E+00, 500, "kgf*m" }, /* *kgf*m */
{ 503, 7.061552E-03, 500, "ozf*in" },
{ 504, 1.129848E-01, 500, "lbf*in" },
{ 505, 1.355818E+00, 500, "lbf*ft" },
/* DENSITY: */
{ 600, 1.000000E+00, 600, "kg/m3" }, /* *kg/m3 */
{ 601, 1.000000E+03, 600, "g/cm3" }, /* *g/cm3 */
{ 602, 7.489152E+00, 600, "ozm/gal" }, /* US liquid */
{ 603, 1.729994E+03, 600, "ozm/in3" },
{ 604, 1.601846E+01, 600, "lbm/ft3" },
{ 605, 2.767990E+04, 600, "lbm/in3" },
{ 606, 1.198264E+02, 600, "lbm/gal" },
{ 607, 5.153788E+02, 600, "slug/ft3" },
/* ENERGY & WORK: */
{ 700, 1.000000E+00, 700, "J" }, /* *(joule) */
{ 701, 1.055056E+03, 700, "Btu" }, /* Btu (Inter. Table) */
{ 702, 4.184000E+00, 700, "cal" }, /* *(thermochemical) */
{ 703, 1.602190E-19, 700, "ev" }, /* electronvolt */
{ 704, 1.000000E-07, 700, "erg" }, /* *erg */
{ 705, 1.355818E+00, 700, "ft*lbf" },
{ 706, 4.214011E-02, 700, "ft*poundal" },
{ 707, 3.600000E+06, 700, "kW*h" },
{ 708, 1.054804E+08, 700, "therm" },
{ 709, 4.184000E+09, 700, "ton-TNT" }, /* nuclear equiv. TNT */
{ 710, 3.600000E+03, 700, "W*h" }, /* *W*h */
/* POWER PER UNIT AREA: */
{ 800, 1.000000E+00, 800, "W/m2" }, /* *W/m2 */
{ 801, 1.135653E+04, 800, "Btu/ft2/s" },
{ 802, 3.154591E+00, 800, "Btu/ft2/h" },
{ 803, 4.184000E+04, 800, "cal/cm2/s" }, /* *(thermochemical) */
{ 804, 1.000000E+04, 800, "W/cm2" }, /* *W/cm2 */
{ 805, 1.550003E+03, 800, "W/in2" },
/* FORCE: */
{ 900, 1.000000E+00, 900, "N" }, /* *(newton) */
{ 901, 1.000000E-05, 900, "dyne" }, /* *dyne */
{ 902, 9.806650E+00, 900, "kgf" }, /* kilogram-force */
{ 903, 4.448222E+03, 900, "kip" }, /* kip (1000 lbf) */
{ 904, 2.780139E-01, 900, "ozf" }, /* ounce-force */
{ 905, 4.448222E+00, 900, "lbf" }, /* lbf */
{ 906, 1.382550E-01, 900, "poundal" },
/* THERMAL CONDUCTIVITY: */
{ 1000, 1.000000E+00, 1000, "W/m/K" }, /* *W/m/K */
{ 1001, 1.730735E+00, 1000, "Btu/h/ft/F" },
{ 1002, 1.442279E-01, 1000, "Btu*in/h/ft2/F"},
{ 1003, 5.192204E+02, 1000, "Btu*in/s/ft2/F"},
/* HEAT CAPACITY: */
{ 1100, 1.000000E+00, 1100, "J/kg/K" }, /* *J/kg/K */
{ 1101, 4.186800E+03, 1100, "Btu/lbm/F" },
/* MASS: */
{ 1200, 1.000000E+00, 1200, "kg" }, /* *kg */
{ 1201, 6.479891E-05, 1200, "gr" }, /* *grain */
{ 1202, 1.000000E-03, 1200, "gm" }, /* gram */
{ 1203, 9.806650E+00, 1200, "kgf*s2/m" },
{ 1204, 2.834952E-02, 1200, "ozm" }, /* ounce-mass */
{ 1205, 4.535924E-01, 1200, "lbm" }, /* pound-mass */
{ 1206, 1.459390E+01, 1200, "slug" },
/* MASS PER UNIT LENGTH: */
{ 1300, 1.000000E+00, 1300, "kg/m" }, /* *kg/m */
{ 1301, 1.111111E-07, 1300, "denier" },
{ 1302, 1.488164E+00, 1300, "lbm/ft" },
{ 1303, 1.785797E+01, 1300, "lbm/in" },
{ 1304, 1.000000E-06, 1300, "tex" }, /* *tex */
/* MASS PER UNIT TIME: */
{ 1400, 1.000000E+00, 1400, "kg/s" }, /* *kg/s */
{ 1401, 1.259979E-04, 1400, "lbm/h" },
{ 1402, 7.559873E-03, 1400, "lbm/min" },
{ 1403, 4.535924E-01, 1400, "lbm/s" },
/* POWER: */
{ 1500, 1.000000E+00, 1500, "W" }, /* *(watt) */
{ 1501, 2.930711E-01, 1500, "Btu/h" },
{ 1502, 1.055056E+03, 1500, "Btu/s" },
{ 1503, 4.184000E+00, 1500, "cal/s" },
{ 1504, 1.000000E+00, 1500, "erg/s" }, /* *erg/s */
{ 1505, 3.766161E-04, 1500, "ft*lbf/h" },
{ 1506, 2.25